<template>
    <el-container>
        <el-header class="header-tabs">
            <el-tabs type="card" v-model="groupId" @tab-change="tabChange">
				<el-tab-pane label="后整待结算" name="0"></el-tab-pane>
				<el-tab-pane label="后整结算单管理" name="1"></el-tab-pane>
			</el-tabs>
        </el-header>
        <el-header v-if="groupId == 0">
            <div class="right-panel-search">
                <el-date-picker v-model="search.time_range" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间" :size="size"  />
                <div class="search-input">合同单号</div>
                <el-input v-model="search.contract_no" placeholder="请输入合同单号" clearable @change="waitUpsearch" />
                <div class="search-input">工厂货号/SKC</div>
                <el-input v-model="search.factory_number" placeholder="工厂货号/SKC" clearable @change="waitUpsearch" style="width:220px" />
                <el-button type="primary" icon="el-icon-search" @click="waitUpsearch">搜索</el-button>
                <el-button type="primary" @click="generate" :disabled="selection.length == 0" v-auth="'sewsettlement.create'">生成结算单</el-button>
            </div>
        </el-header>
        <el-main class="nopadding" v-if="groupId == 0">
            <scTable ref="table" :apiObj="apiObj" @selection-change="selectionChange" stripe remoteSort remoteFilter border :params="query" show-summary>
                <el-table-column type="selection" width="50"></el-table-column>
                <el-table-column fixed label="后整合同号" align="center" width="160">
                    <template #default="{row}">
                        <div>{{row.contract_no}}</div>
                        <div>{{row.create_time}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="商品信息" fixed align="center" width="300">
                    <template #default="{row}">
                        <div class="shopInfo" v-if="row.goods">
                            <el-image class="img" :src="row.goods.goods_image" :preview-src-list="row.goods.goods_image_item" :initial-index="0" fit="cover" preview-teleported="true" hide-on-click-modal />
                            <div class="right">
                                <div>工厂货号：{{row.factory_number}}</div>
                                <div>批次号：{{row.batch_number}}</div>
                                <div>SKC：{{row.skc}}</div>
                                <div>商品品牌：{{row.brand_name}}</div>
                                <div>客户：{{row.custom_name}}</div>
                                <div>颜色：{{row.goods.colour}}</div>
                                <div v-if="row.sizeCate">商品类别：{{row.sizeCate.title}}</div>
                            </div>
                        </div>
                    </template>
                </el-table-column>
                <el-table-column fixed label="尺码" align="center">
                    <template #default="{row}">
                        <div v-for="(item,index) in row.orderDetail" :key="index">
                            {{item.goods_size}}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column fixed label="后整数量" prop="total_weaving_count" align="center">
                    <template #default="{row}">
                        <div v-for="(item,index) in row.orderDetail" :key="index">
                            {{item.weaving_num}}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column fixed label="收货数量" prop="total_complete_count" align="center">
                    <template #default="{row}">
                        <div v-for="(item,index) in row.orderDetail" :key="index">
                            {{item.received_num}}
                        </div>
                    </template>
                </el-table-column>
                <el-table-column label="订单标签" align="center" width="80">
                    <template #default="scope">
                        <el-tag v-if="scope.row.order_label==1" type="danger">加急</el-tag>
                        <el-tag v-else type="success">正常</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="针种" prop="needle_seed_title" align="center" width="100"></el-table-column>
                <el-table-column label="毛种" prop="wool_seed" align="center" width="120"></el-table-column>
                <el-table-column label="工序" prop="produce_process_name" align="center" width="100"></el-table-column>
                <el-table-column label="承制人" align="center" width="100" sortable prop="apply_name" column-key="filterApply" :filters="chengFilters">
					<template #default="{row}">
						<div>{{row.apply_name}}</div>
					</template>
				</el-table-column>
                <el-table-column label="单价" prop="price" align="center" width="100"></el-table-column>
                <el-table-column label="单位" prop="unit_name" align="center" width="100"></el-table-column>
                <el-table-column label="发缝日期" prop="create_time" align="center" width="100"></el-table-column>
                <el-table-column label="完工日期" prop="completed_date" align="center" width="100"></el-table-column>
                <el-table-column label="制单人" prop="creator_name" align="center" width="100"></el-table-column>
            </scTable>
        </el-main>
        <el-header v-if="groupId == 1">
            <div class="right-panel-search">
                <el-date-picker v-model="searchs.time_range" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始时间" end-placeholder="结束时间" :size="size"  />
                <div class="search-input">结算单号</div>
                <el-input v-model="searchs.statement_no" placeholder="请输入结算单号" clearable @change="payUpsearch" />
                <el-select v-model="searchs.confirm_status" placeholder="状态" clearable>
                    <el-option label="未确认" :value="1"/>
                    <el-option label="已确认" :value="2"/>
                </el-select>
                <el-button type="primary" icon="el-icon-search" @click="payUpsearch">搜索</el-button>
            </div>
        </el-header>
        <el-main class="nopadding" v-if="groupId == 1">
            <scTable ref="table" :apiObj="apiObjs" @selection-change="selectionChange" stripe remoteSort remoteFilter border :params="query" show-summary>
                <el-table-column type="selection" width="50"></el-table-column>
                <el-table-column fixed label="结算单号" align="center" width="160" sortable>
                    <template #default="{row}">
                        <div>{{row.statement_no}}</div>
                        <div>{{row.create_time}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="承制人" align="center" width="100" sortable prop="apply_name" column-key="filterApply" :filters="chengFilters">
					<template #default="{row}">
						<div>{{row.apply_name}}</div>
					</template>
				</el-table-column>
                <el-table-column label="结算数量合计" prop="total_count" align="center"></el-table-column>
                <el-table-column label="应付" prop="total_amount" align="center" sortable></el-table-column>
                <!-- <el-table-column label="扣款" prop="deduction_amount" align="center" sortable></el-table-column> -->
                <el-table-column label="补/扣款" prop="deduction_amount" align="center" sortable></el-table-column>
                <el-table-column label="实付" prop="goods_amount" align="center"></el-table-column>
                <el-table-column label="备注" align="center">
                    <template #default="{row}">
                        <div>{{row.remark}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="结算人" prop="creator_name" align="center"></el-table-column>
                <el-table-column label="确认状态" align="center">
                    <template #default="scope">
                        <el-tag v-if="scope.row.confirm_status!=2" type="danger">待确认</el-tag>
                        <el-tag v-else type="success">已确认</el-tag>
                    </template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="90">
                    <template #default="scope">
                        <el-button-group style="display:flex;flex-direction:column">
                            <el-button text type="primary" size="small" @click="showContract(scope.row, scope.$index)" v-auth="'sewsettlement.export'">导出</el-button>
                            <el-button text type="primary" size="small" @click="detail(scope.row, scope.$index)" v-auth="'sewsettlement.detail'">结算明细</el-button>
                            <el-button text type="primary" size="small" @click="goPerfect(scope.row, scope.$index)" v-auth="'sewsettlement.perfect'">完善结算单</el-button>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
        <contract v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></contract>
        <perfect-save v-if="dialog.perfect" ref="perfectDialog" @success="handleSuccess" @closed="dialog.perfect = false" draggable></perfect-save>
    </el-container>
</template>
<script>
import contract from './contract';
import PerfectSave from './perfectSave.vue';
export default {
  components: { contract,PerfectSave, },
	name: 'user',
	data() {
		return {
            dialog:{
                save:false,
                perfect:false,
            },
			group: [],
			apiObj: this.$API.produce.waitSettled.lists,
            apiObjs:this.$API.produce.waitSettled.statementLists,
            query:{
                process_id:8,
            },
			selection: [],
            needleList:[],//针种
			search: {
				time_range: null,
				contract_no: null,
			},
            searchs: {
				time_range: null,
				statement_no: null,
                confirm_status: ""
			},
            groupId:"0",
            chengFilters: [],
            place:4
		}
	},
	mounted() {
        this.getCreate()
    },
	methods: {
        // 生成结算单
        async generate(){
            var contract_no_arr = [];
            this.selection.map((item) => {
                contract_no_arr.push(item.contract_no)
            })
            if(this.search.time_range==null){
                this.$message.error("请选择时间")
            }else{
                let start_date = this.search.time_range[0]
                let end_date = this.search.time_range[1]
                var res = await this.$API.produce.waitSettled.createStatementOrder.post({contract_no_arr:contract_no_arr,start_date:start_date,end_date:end_date,process_id:8});
                if (res.code == 1) {
                    this.$message.success("操作成功");
                    this.$refs.table.refresh()
                } else {
                    this.$alert(res.message, "提示", { type: "error" });
                }
            }
        },
        // 付款
        async goPayment(row){
            var res = await this.$API.produce.setConfirm.payment.post({statement_no:row.statement_no});
            if (res.code == 1) {
                this.$message.success("操作成功");
                this.$refs.table.refresh()
            } else {
                this.$alert(res.message, "提示", { type: "error" });
            }
        },
        // 结算明细
        detail(row){
            this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
        },
        // 完善结算单
        goPerfect(row){
            this.dialog.perfect = true
			this.$nextTick(() => {
				this.$refs.perfectDialog.open('edit').setData(row)
			})
        },
        async showContract(row) {
			var reqData = { statement_no: row.statement_no};
			var res = await this.$API.produce.waitSettled.selExport.get(reqData);
            if (res.code == 1) {
                if(res.data.rows !=''){
                    const fileUrl = res.data.rows.url;
                    const link = document.createElement("a");
                    link.href = fileUrl;
                    link.download = fileUrl;
                    link.click();
                }else{
                    this.$alert("请求异常", "提示", { type: "error" });
                }
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
		//搜索
		waitUpsearch() {
			this.$refs.table.upData(this.search)
		},
        payUpsearch() {
			this.$refs.table.upData(this.searchs)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'set') {
				this.$refs.table.refresh()
			}
		},
        selectionChange(selection) {
			this.selection = selection;
		},
        tabChange(e){
            this.groupId = e
            for (const key in this.search) {
                this.search[key] = null;
            }
            if(e==0){
                this.place = 4
            }else{
                this.place = 5
            }
            this.getCreate()
        },
        // 下单人/制单人
        async getCreate() {
            var res = await this.$API.all.getProduceFilter.get({process_id:8,place:this.place});
			let c = []
            res.data.supplier_list.map(item => {
                c.push({
                    text: item.name,
                    value: item.id,
                })
            })
			this.chengFilters = c
		},
        handleKeyDown() {
            this.waitUpsearch()
        },
        handleKeyDowns() {
            this.payUpsearch()
        },
	}
}
</script>

<style>
.search-input {
	width: auto;
	font-size: 14px;
}
</style>